package com.ykzb.project.service.impl;

import com.github.pagehelper.PageInfo;
import com.ykzb.base.bean.ResultBean;
import com.ykzb.project.dao.ICategoryDao;
import com.ykzb.project.dao.IProjectBidDao;
import com.ykzb.project.dao.IProjectDao;
import com.ykzb.project.model.CategoryInfo;
import com.ykzb.project.model.ProjectBid;
import com.ykzb.project.model.ProjectInfo;
import com.ykzb.project.service.IProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mac on 2017/3/30.
 */
@Service
public class ProjectServiceImpl implements IProjectService{

    @Autowired
    private IProjectDao projectDao;

    @Autowired
    private IProjectBidDao projectBidDao;

    /**
     *
     * @param minMoney 最小金额
     * @param maxMoney 最大金额
     * @param state    项目状态
     * @param category 分类
     * @param currPage 当前页
     * @param pageSize 每页条数
     * @return
     * @throws Exception
     */
    @Override
    public ResultBean searchProject(String minMoney, String maxMoney, String state, String category, String currPage, String pageSize) {
        ResultBean ret = new ResultBean();
        try{
            if (StringUtils.isEmpty(currPage) ){
                currPage = "1";
            }
            if (StringUtils.isEmpty(pageSize)){
                pageSize = "10";
            }
            PageInfo<ProjectInfo> pageInfo = projectDao.selectProject(minMoney,maxMoney,state,category,currPage,pageSize);
            ret.setSuccess(true);
            ret.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            ret.setSuccess(false);
        }
        return ret;
    }


    /**
     * 跳到项目详情页
     * @return
     */
    @Override
    public ModelMap toProjectInfo(String projectId) throws Exception{
        ModelMap model = new ModelMap();
        List<ProjectInfo> recomendProjectList = projectDao.selectRecomendProject(3);

        Long proId = null;
        if (StringUtils.isNotEmpty(projectId)){
            proId = Long.parseLong(projectId);
        }
        ProjectInfo project = projectDao.selectProjectById(proId);
        List<ProjectBid> projectBidList = projectBidDao.selectProjectBidByProjectId(proId);
        model.addAttribute("recomendList",recomendProjectList);
        model.addAttribute("bidList",projectBidList);
        model.addAttribute("projectInfo",project);
        return model;
    }

}
